package com.wzy.shirostudy.configurer;

import com.wzy.shirostudy.exception.MyExceptionResolver;
import com.wzy.shirostudy.filter.CorsFilter;
import com.wzy.shirostudy.filter.JwtFilter;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

/**
 * @program: shiro-study
 * @description: shiro的配置文件
 * @author: 1
 * @create: 2020-04-15 16:01
 **/
@Configuration
public class ShiroConfigurer {




    /**
     * 下面的代码是添加注解支持
     */
    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        // 强制使用cglib，防止重复代理和可能引起代理出错的问题
        // https://zhuanlan.zhihu.com/p/29161098
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;
    }

    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }


    /**
     * 注入 securityManager
     */
    @Bean
    public DefaultWebSecurityManager securityManager(ShiroCustomRealm shiroCustomRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 设置realm
        securityManager.setRealm(shiroCustomRealm);

        /**
         * 关闭shiro自带的session，详情见文档
         * http://shiro.apache.org/session-management.html#SessionManagement-StatelessApplications%28Sessionless%29
         */
        DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();

        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);

        subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        securityManager.setSubjectDAO(subjectDAO);

        return securityManager;
    }


    /**
     * 将shiro过滤器交给spring管理
     * @param securityManager
     * @return
     */
    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        //加入  安全管理器 管理所有Subject 相当于整体的管理人。
        factoryBean.setSecurityManager(securityManager);
        factoryBean.setLoginUrl("/user/login");
        /**
         * 设置无权访问的地址 当使用注解时这个地址就会失效,所以需要重写异常类
         * 可查看 {@link MyExceptionResolver}
         */
        factoryBean.setUnauthorizedUrl("/permission/not");


        // 添加自己的过滤器并且取名为jwt
        Map<String, Filter> filterMap = new HashMap<>(4);
        filterMap.put("jwt", new JwtFilter());
        filterMap.put("cors", new CorsFilter());
        factoryBean.setFilters(filterMap);

        //基本系统级别权限配置
        Map<String, String> filterRuleMap = new HashMap<>();
        // druid
        filterRuleMap.put("/druid/**", "anon");
        // 开放登陆接口
        filterRuleMap.put("/user/login", "anon");
        // 所有请求通过我们自己的JWT Filter  这里没有配置会导致配置
        // org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
        filterRuleMap.put("/**", "cors, jwt");
        factoryBean.setFilterChainDefinitionMap(filterRuleMap);
        return factoryBean;
    }

}
